package com.ysd.lis.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysd.lis.aspect.SysExceptionalLog;
import com.ysd.common.Result;
import com.ysd.lis.entity.SysUser;
import com.ysd.lis.mapper.sys.SysExceptionalLogMapper;
import com.ysd.lis.request.ExceptionalLogPageParam;
import com.ysd.lis.service.SysExceptionalLogService;
import com.ysd.util.RedisUserManager;
import com.ysd.util.ToolsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * cold_chain.sys_module 服务实现类
 * </p>
 *
 * @author Bai
 * @since 2023-11-06
 */
@Service
public class SysExceptionalLogServiceImpl extends ServiceImpl<SysExceptionalLogMapper, SysExceptionalLog> implements SysExceptionalLogService {
    @Autowired
    SysExceptionalLogMapper exceptionalLogMapper;

    public Result getPageExceptionalLogging(ExceptionalLogPageParam exceptionalLogPageParam) {
        Map returnMap = new HashMap<>();
        LambdaQueryWrapper<SysExceptionalLog> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        Page<SysExceptionalLog> page = new Page<SysExceptionalLog>();
        page.setCurrent(exceptionalLogPageParam.getPageIndex());
        page.setSize(exceptionalLogPageParam.getPageSize());
        // lambdaQueryWrapper.eq(SysModule::getDelFlag, 0);
        SysUser sysUser = RedisUserManager.getUser();
        if (StringUtils.isNotBlank(exceptionalLogPageParam.getOrgId())) {
            lambdaQueryWrapper.eq(SysExceptionalLog::getOrgId, exceptionalLogPageParam.getOrgId());
        }
        if (StringUtils.isNotBlank(exceptionalLogPageParam.getUserName())) {
            lambdaQueryWrapper.eq(SysExceptionalLog::getUserName, exceptionalLogPageParam.getUserName());
        }
        if (StringUtils.isNotBlank(exceptionalLogPageParam.getServiceName())) {
            lambdaQueryWrapper.eq(SysExceptionalLog::getServiceName, exceptionalLogPageParam.getServiceName());
        }
        if (ToolsUtils.isNotEmpty(exceptionalLogPageParam.getBeginLog()) && ToolsUtils.isNotEmpty(exceptionalLogPageParam.getEndLog())) {
            lambdaQueryWrapper.ge(SysExceptionalLog::getOperateTime, new Timestamp(DateUtil.parse(exceptionalLogPageParam.getBeginLog()).getTime()))
                    .le(SysExceptionalLog::getOperateTime, new Timestamp(DateUtil.parse(exceptionalLogPageParam.getEndLog()).getTime()));
        }
        if(ToolsUtils.isNotEmpty(exceptionalLogPageParam.getMachineIp())){
            lambdaQueryWrapper.eq(SysExceptionalLog::getMachineIp, exceptionalLogPageParam.getMachineIp());
        }
        lambdaQueryWrapper.orderByDesc(SysExceptionalLog::getCreateTime);
        lambdaQueryWrapper.select(SysExceptionalLog::getId,
                SysExceptionalLog::getOperateTime,
                SysExceptionalLog::getCreateTime,
                SysExceptionalLog::getHospitalName,
                SysExceptionalLog::getModule,
                SysExceptionalLog::getOperationType,
                SysExceptionalLog::getServiceName,
                SysExceptionalLog::getUserName,
                SysExceptionalLog::getHospitalCode,
                SysExceptionalLog::getMachineIp,
                SysExceptionalLog::getMachineName,
                SysExceptionalLog::getContent,
                SysExceptionalLog::getRequestMethod,
                SysExceptionalLog::getRequestUrl,
                SysExceptionalLog::getMethod,
                SysExceptionalLog::getTakeTime,
                SysExceptionalLog::getErrorMsg
        );
        Page<SysExceptionalLog> logPageList = exceptionalLogMapper.selectPage(page, lambdaQueryWrapper);
        returnMap.put("logPageList", logPageList);
        return Result.succ(1, "获取异常日志信息成功", returnMap);
    }
}
